package com.lancoo.dictionary.data.adapter;

import java.util.List;

import com.lancoo.dictionary.R;
import com.lancoo.dictionary.data.model.SampleSentence;
import com.lancoo.dictionary.utils.XBaseViewHolder;

import android.support.v7.widget.RecyclerView.Adapter;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

public class CommonUsageAdapter extends Adapter<CommonUsageAdapter.CommonUsageViewHolder> {

	private List<SampleSentence> commonUsageSentences;
	private SearchResultAdapter searchResultAdapter;

	static class CommonUsageViewHolder extends XBaseViewHolder {

		private TextView itemSentence;
		private ImageButton itemPlaySentence;

		public CommonUsageViewHolder(View itemView) {
			super(itemView);
			itemSentence = (TextView) itemView.findViewById(R.id.item_sentence);
			itemPlaySentence  = (ImageButton) itemView.findViewById(R.id.item_play_sentence);
		}
	}

	public CommonUsageAdapter(List<SampleSentence> commonUsageSentences, SearchResultAdapter searchResultAdapter) {
		this.searchResultAdapter = searchResultAdapter;
		this.commonUsageSentences = commonUsageSentences;
	}

	@Override
	public int getItemCount() {
		return commonUsageSentences.size();
	}

	@Override
	public void onBindViewHolder(CommonUsageViewHolder arg0, int arg1) {
		SampleSentence sampleSentence = commonUsageSentences.get(arg1);
		StringBuilder sb = new StringBuilder();
		sb.append(sampleSentence.getSentenceEn()).append("<BR/>").append(sampleSentence.getsTranslation());
		arg0.itemSentence.setText(Html.fromHtml(sb.toString()));
		String soundUri = sampleSentence.getsViocePath();
		if (TextUtils.isEmpty(soundUri)) {
			arg0.itemPlaySentence.setVisibility(View.INVISIBLE);
		} else {
			arg0.itemPlaySentence.setVisibility(View.VISIBLE);
			searchResultAdapter.bindPlaySoundsListener(arg0.itemPlaySentence, sampleSentence.getsViocePath());
		}
	}

	@Override
	public CommonUsageViewHolder onCreateViewHolder(ViewGroup viewgroup, int i) {
		return new CommonUsageViewHolder(
				LayoutInflater.from(viewgroup.getContext()).inflate(R.layout.lcdic_item_sentence, null));
	}
}
